<?php

class Ajax extends App {
	public static $totalscr;
	
	public static function Init() {
		global $cl, $conf;
		Page::JS("ajax.js");
		Page::CSS("ajax.css");
		
		$code = "<script type=\"text/javascript\">\n// Ajax Call Definitions\n";
		$code .= "$.rooturl='".$cl['relurl']."';\n";
		if (is_Array(App::$Calls["ajax"])) {
			foreach (App::$Calls["ajax"] as $app => $calls) {
				foreach ($calls as $call => $tocall) {
					$code .= "function ".strtolower($app)."_".strtolower($call)."() { minf_ajax('$app', '$call', arguments); }\n";
				}
			}
		}
		$code .= "</script>\n";
//		Page::Set("page.append", "hey");
//		if ($conf['ajax']['indicator']=="true")
			Page::Set("page.append", smarty_render("ajax.tpl"));
		Page::Set("page.head", $code);
	}
	
	public static function HandleCall() {
		$r = func_get_args();
				
		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
		header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		//header("Cache-Control: no-cache");
		//header("Expires: -1");
		header("Content-Type: text/javascript");
		
		echo "/*";
		$GLOBALS['no-output'] = true;	// We should now, wish for other app/calls checks this :
		$arguments = unserialize(str_replace("\\\"", "\"", $_POST['args']));
		foreach ($arguments as $k => $v) $arguments[$k] = base64_decode($v);
		// DO THE JOB OFCOURSE
			$data = App::CallSeq($r[0], $r[1], $arguments, "ajax", "std");
		echo "*/";
		
		if ($data['return']) self::Script("$.ajaxreturn=unserialize(\"".self::Escape(serialize($data['return']))."\");");

		// Get the script cache
		if (Page::$Vars["page"]["script"]) self::$totalscr .= Page::$Vars["page"]["script"];

		// Get Errors and Mysql Queries
		if (Auth::Check("developer")&&class_exists('Debugpanel')) {
			global $messages;
			$mymsg = array();
			foreach ($messages as $m)
				if ($m["level"]!=MSG_NOTICE) {
					$m['time'] = "Ajax";
					$mymsg[] = $m;
				}

			$msgcode = Debugpanel::RenderMessages($mymsg);
			self::Script("$('#debugpanelic ul').append(\"".self::Escape($msgcode)."\");");
		}

		// PRINT OUT THE SCRIPT
		echo self::$totalscr;
	}
	/*
	public static function ServeJS() {
		header("Content-Type: application/javascript");
		$GLOBALS['no-output'] = true;
		echo "// Minf Provided File\n";
	 	echo get_file("ajax.js");
	}
	*/
	public static function Html($Selector, $Value) {
		self::Script("$('$Selector').html(\"".self::Escape($Value)."\");");
	}
	
	public static function Growl ($msg) {
		self::Script("$.jGrowl(\"".self::Escape($msg)."\");");
	}
	
	public static function wGrowl ($msg) {
		self::Script("$.jGrowl(\"".self::Escape($msg)."\", {theme:'white'});");
	}
	
	public static function Modal ($html, $openlinksinmodal = true) {
		self::Script("modalbox_show(\"".self::Escape($html)."\", ".($openlinksinmodal?1:0).");");
	}
	
	public static function Redirect ($url, $timeout=null) {
		$url = (substr($url,0,4)=="http") ? $url : root().$url;
		if ($timeout)
			self::Script("setTimeout(\"document.location='{$url}';\", {$timeout});");
		else
			self::Script("document.location='{$url}';");
	}
	
	public static function Script($Code) {
		self::$totalscr .= $Code;
	}
	
	public static function Escape($code) {
		$replace = array(
			"\\"=>"\\\\",
			"\n"=>"\\n",
			"\r"=>"\\r",
			"\""=>"\\\""
			);
		return str_replace(array_keys($replace), array_values($replace), $code);
	}
	
	public static function Popup($code, $width = null) {
		if (!$width) $width = 0;
		self::Script("ajax_open_popup(\"".self::Escape($code)."\", $width);");
	}
}

?>